<?php
	require 'lib/mysql.php';

	define('NUM_SUB_ITEM', 2);

	$origin = 'select subitemid, question_stem from subjective_item order by rand() limit ' . NUM_SUB_ITEM;

	if(!mysqli_stmt_prepare($mysqli_stmt, $origin)) {
		die('Statement Preparation Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}

	if(!mysqli_stmt_execute($mysqli_stmt)) {
		die('Statement Execution Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt)); 
	}

	if(!mysqli_stmt_bind_result($mysqli_stmt, $subitem_id, $question_stem)) {
		die('Statement Result Binding Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}

	if(!mysqli_stmt_store_result($mysqli_stmt)) {
		die('Statement Storing Result Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}

	$i = 0;
	while(mysqli_stmt_fetch($mysqli_stmt)) {
		$i++;
		$itemno = 'subitem' . $i;
		$itemid = 'subitem' . $i . 'id';
?>

	<div class="item">
		<p><?php printf('%d . %s', $i, $question_stem); ?></p>
		<textarea name="<?= $itemno ?>" rows=20 cols=100></textarea>
		<input type="hidden" name="<?= $itemid ?>" value="<?= $subitem_id ?>"/>
	</div>

<?php
	}

	mysqli_stmt_close($mysqli_stmt);
	mysqli_close($mysqli);
?>
